package com.util;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ReadResourceFileUtil {
    /**
     * 读取资源文件为byte
     *
     * @param resourceFilePath 传入的直接是resource文件目录下的路径以及名称
     * @return 返回这个文件的byte数组
     */
    public static byte[] readToByte(String resourceFilePath) {
        InputStream inputStream = ReadResourceFileUtil.class.getClassLoader().getResourceAsStream(resourceFilePath);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = Objects.requireNonNull(inputStream).read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                assert inputStream != null;
                inputStream.close();
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取资源文件为list
     *
     * @param resourceFileName 资源文件下路径
     * @param encode           读取字符集
     * @return 返回内容 为List集合
     */
    public static List<String> readToList(String resourceFileName, Charset encode) {
        List<String> lines = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            Objects.requireNonNull(ReadResourceFileUtil.class.getClassLoader().getResourceAsStream(resourceFileName)),
                            encode));
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    /**
     * 默认使用UTF-8编码读取
     */
    public static List<String> readToList(String filePath) {
        return readToList(filePath, StandardCharsets.UTF_8);
    }

    public static List<String> readToList(String filePath, String prefixStr) {
        List<String> list = readToList(filePath, StandardCharsets.UTF_8);
        list = list.stream().map(s -> prefixStr + s).collect(Collectors.toList());
        return list;
    }

    /**
     * 从资源文件夹加载配置文件并转换为Map
     *
     * @param resourcePath 资源文件路径（相对于resources目录）
     * @return 包含配置信息的Map
     */
    public static Map<String, String> loadConfigFromResource(String resourcePath) {
        Map<String, String> configMap = new HashMap<>();
        try (InputStream inputStream = ReadResourceFileUtil.class.getClassLoader().getResourceAsStream(resourcePath)) {
            if (inputStream == null) {
                return null;
            }
            Properties properties = new Properties();
            properties.load(inputStream);

            // 将Properties转换为Map
            for (String key : properties.stringPropertyNames()) {
                configMap.put(key, properties.getProperty(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return configMap;
    }

    public static void main(String[] args) {
        Map<String, String> stringStringMap = loadConfigFromResource("config.properties");
        System.out.println(stringStringMap.get("printType"));
    }
}
